package in.muton.edushare.panels.upload.uploadify;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.resources.CompressedResourceReference;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;

public class UploadifyBehavior implements Serializable {

	private static final long serialVersionUID = 6704203883341983400L;
	
	private String fileExt = "";
	private String fileDesc = "";
	private String sizeLimit = "";
	private String scriptData = "";

	private final String formSessionId;
	private Class<? extends UploadifyProcessPage> fileProcessPageClass;

	public UploadifyBehavior(Class<? extends UploadifyProcessPage> uploadifyPage, String formSessionId){
		this.fileProcessPageClass = uploadifyPage;
		this.formSessionId = formSessionId;
	}
	
	protected IModel getVariables(){
		
		return new AbstractReadOnlyModel<Map<String, CharSequence>>(){

			private static final long serialVersionUID = -6966209197006317078L;
			
			@Override
			public HashMap<String, CharSequence> getObject() {
				HashMap<String, CharSequence> variables = 
					new HashMap<String, CharSequence>();
				
				variables.put("uploader", RequestCycle.get().urlFor(new CompressedResourceReference(UploadifyPanel.class, "uploadify.swf")));
				variables.put("cancelImg", RequestCycle.get().urlFor(new CompressedResourceReference(UploadifyPanel.class, "cancel.png")));
				PageParameters params = new PageParameters();
				params.add("param1", "val1");
				/*String uploadURL = RequestCycle.get().urlFor(fileProcessPageClass, params).toString();
				if(uploadURL.contains("?"))
				{
					int paramStarts = uploadURL.toString().indexOf('?');
					uploadURL = uploadURL.subSequence(0, paramStarts)+ ";jsessionid=" + formSessionId + uploadURL.substring(paramStarts);
				}else{
					uploadURL = uploadURL+";jsessionid=" + formSessionId;
				}*/
				variables.put("script", ";jsessionid=" + formSessionId + RequestCycle.get().urlFor(fileProcessPageClass, null));
				
				//Optional configuration
				variables.put("fileExt", fileExt);
				variables.put("fileDesc", fileDesc);
				variables.put("sizeLimit", sizeLimit);
				variables.put("scriptData", getScriptData());
				 
				return variables;
			}
			
		};
	}
	

	/**
	 * Sets allowed file extensions.
	 * Accepted formal *.doc;*.pdf;*.jpg
	 * @param fileExt
	 */
	public void setFileExt(String fileExt) {
		this.fileExt = fileExt;
	}

	public String getFileExt() {
		return fileExt;
	}

	/**
	 * Sets message to be shown while selecting files
	 * e.g. "Select files of format doc, pdf, jpg"
	 * @param fileDesc
	 */
	public void setFileDesc(String fileDesc) {
		this.fileDesc = fileDesc;
	}

	public String getFileDesc() {
		return fileDesc;
	}
	
	/**
	 * Set size limit in Bytes
	 * @param sizeLimit
	 */
	public void setSizeLimit(String sizeLimit) {
		this.sizeLimit = sizeLimit;
	}

	public String getSizeLimit() {
		return sizeLimit;
	}

	/**
	 * parameters to be passed in "'param1':'value1', 'param2':'value2'" format
	 * @param scriptData
	 */
	public void setScriptData(String scriptData) {
		this.scriptData = scriptData;
	}

	public String getScriptData() {
		return scriptData;
	}
}
 